﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using homework5;

namespace OrderWinform
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        public OrderService os;
        public String SearchText { get; set; }
        public Action<FormEditOrder> ShowEditForm { get; set; }
        public Form1()
        {
            InitializeComponent();
            Goods g1 = new Goods("鞋子", 3, 500);
            Goods g2 = new Goods("衣服", 2, 100);
            Goods g3 = new Goods("包包", 1, 300);

            List<Goods> goods1 = new List<Goods> { g1, g2 };
            List<Goods> goods2 = new List<Goods> { g3 };
            OrderDetails od1 = new OrderDetails(goods1);
            Order order1 = new Order(new Customer("zhang", "女", 18, "12000000000"),1,od1);
            OrderDetails od2 = new OrderDetails(goods2);
            Order order2 = new Order(new Customer("wang", "男 ", 18, "12000000001"), 2, od2);
            os = new OrderService(new List<Order> { order1, order2 });

            OrderBindingSource.DataSource = os.Orders;
            txtSearch.DataBindings.Add("Text", this, "SearchText");
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            //按ID查询
            if (rbID.Checked)
            {
                int.TryParse(SearchText, out int id);
                Order order = os.SearchByNum(id);
                List<Order> result = new List<Order>();
                if (order != null)
                    result.Add(order);
                OrderBindingSource.DataSource = result;
            }
            //按顾客查询
            if(rbCustomer.Checked)
            {
                Order order = os.SearchByCustomer(SearchText);
                List<Order> result = new List<Order>();
                if (order != null)
                    result.Add(order);
                OrderBindingSource.DataSource = result;
            }
            OrderBindingSource.ResetBindings(false);
        }
        //点击order表
        private void dgvOrder_MouseClick(object sender, MouseEventArgs e)
        {
            string select = dgvOrder.CurrentRow.Cells[0].Value.ToString();
            int selectedID = Convert.ToInt32(select);
            Order order = os.SearchByNum(selectedID);
            if(order!=null&&order.Details!=null)
                OrderDetailsBindingSource.DataSource = order.Details.Good;
            else
                OrderDetailsBindingSource.DataSource = null;

        }
        //添加订单按钮
        private void btnAddOrder_Click(object sender, EventArgs e)
        {
            FormAddOrder formAddOrder = new FormAddOrder(os);
            formAddOrder.ShowDialog();
            this.OrderBindingSource.ResetBindings(false);
        }
        //修改订单按钮
        private void btnEdit_Click(object sender, EventArgs e)
        {
            string select = dgvOrder.CurrentRow.Cells[0].Value.ToString();
            int selectedID = Convert.ToInt32(select);
            Order order = os.SearchByNum(selectedID);
            using (FormEditOrder formEditOrder = new FormEditOrder(os, order))
            {
                formEditOrder.ShowDialog();
            }
            this.OrderBindingSource.ResetBindings(false);
        }
        //导出订单
        private void btnExport_Click(object sender, EventArgs e)
        {
            DialogResult result = saveFileDialog1.ShowDialog();
            if (result.Equals(DialogResult.OK))
            {
                String fileName = saveFileDialog1.FileName;
                os.Export(fileName);
            }
        }
        //导入订单
        private void btnImport_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result.Equals(DialogResult.OK))
            {
                String fileName = openFileDialog1.FileName;
                os.Import(fileName);
            }
        }
        //删除订单
        private void btnDelete_Click(object sender, EventArgs e)
        {
            string select = dgvOrder.CurrentRow.Cells[0].Value.ToString();
            int selectedID = Convert.ToInt32(select);
            os.Delete(selectedID);
            this.OrderBindingSource.ResetBindings(false);
        }
        //修改明细
        private void dvgOrderDetails_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string select = dgvOrder.CurrentRow.Cells[0].Value.ToString();
                int selectedID = Convert.ToInt32(select);
                Order order = os.SearchByNum(selectedID);
                List<Goods> goods = new List<Goods> { };
                for (int i = 0; i < dvgOrderDetails.Rows.Count - 1; i++)
                {
                    Goods g = new Goods(dvgOrderDetails.Rows[i].Cells[0].Value.ToString(),
                        Convert.ToInt32(dvgOrderDetails.Rows[i].Cells[1].Value),
                        Convert.ToDouble(dvgOrderDetails.Rows[i].Cells[2].Value));
                    g.Money = g.Num * g.Price;
                    goods.Add(g);
                }
                os.Amend(order, new OrderDetails(goods));
                order.Money = new OrderDetails(goods).Money;
            }catch(Exception ex)
            {
                MessageBox.Show("输入不正确");
            }
            OrderBindingSource.ResetBindings(false);
            OrderDetailsBindingSource.ResetBindings(false);
        }
        //删除明细
        private void btnOrderDetailsDelete_Click(object sender, EventArgs e)
        {
            string select = dgvOrder.CurrentRow.Cells[0].Value.ToString();
            int selectedID = Convert.ToInt32(select);
            Order order = os.SearchByNum(selectedID);
            List<Goods> list = order.Details.Good;
            Goods goods = list.Where(good => good.Name == dvgOrderDetails.CurrentRow.Cells[0].Value.ToString()).FirstOrDefault();
            if (goods != null)
                list.Remove(goods);
            order.Money = new OrderDetails(list).Money;
            OrderBindingSource.ResetBindings(false);
            OrderDetailsBindingSource.ResetBindings(false);
        }
    }
    

}
